package site.wtcc.originalfavor.biz.service.osUser.impl;

import site.wtcc.originalfavor.func.constant.enums.UserLevel;
import site.wtcc.originalfavor.func.constant.enums.UserSex;
import site.wtcc.originalfavor.dao.entity.osUser.OsUser;
import site.wtcc.originalfavor.dao.osUser.OsUserMapper;
import site.wtcc.originalfavor.dao.entity.osUser.request.OsUserPageRequest;
import site.wtcc.originalfavor.biz.service.osUser.IOsUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hxd
 * @since 2020-11-05
 */
@Service
public class OsUserServiceImpl extends ServiceImpl<OsUserMapper, OsUser> implements IOsUserService {

    @Override
    public IPage<OsUser> findListByPage(Integer page, Integer pageCount){
        IPage<OsUser> pages = new Page<>(page, pageCount);
        OsUser param = new OsUser();
        return baseMapper.selectPage(pages, Wrappers.query(param));
    }

    @Override
    public int add(OsUser request){
        request.setUserLevel(UserLevel.USER.getLevelChar());
        request.setUserSex(UserSex.NOT_KNOW.getCode());
        request.setCreateTime(LocalDateTime.now());
        request.setUpdateTime(LocalDateTime.now());
        request.setDeleteTag(0);

        return baseMapper.insert(request);
    }

    @Override
    public int delete(Long id){
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(OsUser request){
        return baseMapper.updateById(request);
    }

    @Override
    public OsUser getPageById(Integer id){
        return baseMapper.selectById(id);
    }

    @Override
    public IPage<OsUser> getPage(OsUserPageRequest request){
        IPage pages = new Page<>(request.getPage(), request.getPageCount());
        //todo findPage 带page参数查询
        List<OsUser> list = baseMapper.findList(request);
        pages.setRecords(list);

        return pages;
    }

    @Override
    public IPage<OsUser> login(OsUserPageRequest request) {
        IPage pages = new Page<>(request.getPage(), request.getPageCount());
        List<OsUser> list = baseMapper.findList(request);
        if (list.size() >= 1) {
            pages.setRecords(list);
            return pages;
        } else {
            return null;
        }
    }
}
